﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using DevExpress.XtraReports.UI;

namespace Baocao
{
    public partial class BaoCao_NXT : System.Web.UI.Page
    {
        private SqlConnection appConn;
        private System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("vi-VN");
        public static string TenCty = "CÔNG TY CỔ PHẦN HIVI", DcCty = "ĐC:38 - Trường Chinh - Hà Nội", DtCty = "ĐT:(04)32343242", Loaipm = "Phần mềm bán hàng", Tenpm = "HIVI 2S PRO Mobie", Webpm = "www.hivi.com.vn";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["appConn"] != null)
            {
                appConn = (SqlConnection)Session["appConn"];
                lbError.Text = "";
                if (!IsPostBack)
                {
					Baocao.reportpage yourMaster = (Baocao.reportpage)this.Master;
					yourMaster.ChangeTitle("Báo cáo nhập xuất tồn");

                    dttuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    dtdenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");

                    drnhomhang.DataSource = getNhomHang();
                    drnhomhang.DataValueField = "Nhvt2ID";
                    drnhomhang.DataTextField = "Nhvt2Name";
                    drnhomhang.DataBind();

                    drnhomkhohang.DataSource = getNhomKhoHang();
                    drnhomkhohang.DataValueField = "NhkhoID";
                    drnhomkhohang.DataTextField = "NhkhoName";
                    drnhomkhohang.DataBind();

                    drnhomvuviec.DataSource = getNhomVuViec();
                    drnhomvuviec.DataValueField = "NhvvID";
                    drnhomvuviec.DataTextField = "NhvvName";
                    drnhomvuviec.DataBind();
                }
            }
            else
            {
                Response.Redirect("~\\Login.aspx");
            }

        }


        protected void btIn_Click(object sender, EventArgs e)
        {
            string ngaybd_bc = "", ngaykt_bc = "", nhom_hang = "", ma_hang = "", nhom_kho = "", ma_kho = "", nhom_vv = "", ma_vv = "", ngaybd = "", ngaykt = "";
            int ps = 0, ton = 0;
            try
            {
                if (!dttuNgay.Text.ToString().Equals(""))
                {
                    ngaybd_bc = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("yyyyMMdd");
                    ngaybd = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("dd/MM/yyyy");
                }
                if (!dtdenNgay.Text.ToString().Equals(""))
                {
                    ngaykt_bc = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("yyyyMMdd");
                    ngaykt = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("dd/MM/yyyy");
                }
                if (drnhomhang.SelectedValue.ToString() != "")
                {
                    nhom_hang = drnhomhang.SelectedValue;
                }
                if (drnhomkhohang.SelectedValue.ToString() != "")
                {
                    nhom_kho = drnhomkhohang.SelectedValue;
                }
                if (drnhomvuviec.SelectedValue.ToString() != "")
                {
                    nhom_vv = drnhomvuviec.SelectedValue;
                }
                if (drkhohang.SelectedValue.ToString() != "")
                {
                    ma_kho = drkhohang.SelectedValue;
                }
                if (drvuviec.SelectedValue.ToString() != "")
                {
                    ma_vv = drvuviec.SelectedValue;
                }
                if (ckmathangton.Checked)
                    ton = 1;
                if (ckps.Checked)
                    ps = 1;
                ma_hang = txtMaHangHoa.Text;
                DataSet ds = new DataSet();

                if (drmauin.SelectedValue == "R0048")
                {
                    string s = "EXEC [fs_HG_BCTHNXT]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', N'" + ma_hang + "',  N'" + nhom_hang + "',N'" + ma_kho + "',  N'" + nhom_kho + "',N'" + ma_vv + "',N'" + nhom_vv + "', " + ps + ", " + ton + "";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);

                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        m_rptTHNXT rpt = new m_rptTHNXT { DataSource = ds };
                        rpt.Ten_hang.DataBindings.Add("Text", ds, "Ten_vt");
                        rpt.Ton_dau.DataBindings.Add("Text", ds, "Ton_dau", "{0:n0}");
                        rpt.Du_dau.DataBindings.Add("Text", ds, "Du_dau", "{0:n0}");
                        rpt.SL_Nhap.DataBindings.Add("Text", ds, "sl_nhap", "{0:n0}");
                        rpt.Tien_nhap.DataBindings.Add("Text", ds, "Tien_nhap", "{0:n0}");
                        rpt.Sl_Xuat.DataBindings.Add("Text", ds, "sl_xuat", "{0:n0}");
                        rpt.Tien_xuat.DataBindings.Add("Text", ds, "Tien_xuat", "{0:n0}");
                        rpt.Ton_cuoi.DataBindings.Add("Text", ds, "Ton_cuoi", "{0:n0}");
                        rpt.Du_cuoi.DataBindings.Add("Text", ds, "Du_cuoi", "{0:n0}");
                        rpt.Parameters["tungay"].Value = ngaybd;
                        rpt.Parameters["denngay"].Value = ngaykt;
                        if (drkhohang.SelectedValue == "")
                        {
                            rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";
                        }
                        else
                        {
                            rpt.Parameters["Kho"].Value = "KHO: " + drkhohang.SelectedValue + " - " + drkhohang.SelectedItem.Text.ToString();
                        }
                        rpt.Parameters["tenCty"].Value = TenCty;
                        rpt.Parameters["dcCty"].Value = DcCty;
                        rpt.Parameters["dtCty"].Value = DtCty;

                        rpt.Parameters["Tenpm"].Value = Tenpm;
                        rpt.Parameters["Loaipm"].Value = Loaipm;
                        rpt.Parameters["Webpm"].Value = Webpm;
                        lbError.Text = "";
                        Session["Report"] = rpt;
                    }
                    else
                    {
                        rptTHNXT rpt = new rptTHNXT { DataSource = ds };
                        rpt.Ma_hang.DataBindings.Add("Text", ds, "Ma_vt");
                        rpt.Ten_hang.DataBindings.Add("Text", ds, "Ten_vt");
                        rpt.Dvt.DataBindings.Add("Text", ds, "Dvt");
                        rpt.Ton_dau.DataBindings.Add("Text", ds, "Ton_dau", "{0:n0}");
                        rpt.Du_dau.DataBindings.Add("Text", ds, "Du_dau", "{0:n0}");
                        rpt.SL_Nhap.DataBindings.Add("Text", ds, "sl_nhap", "{0:n0}");
                        rpt.Tien_nhap.DataBindings.Add("Text", ds, "Tien_nhap", "{0:n0}");
                        rpt.Sl_Xuat.DataBindings.Add("Text", ds, "sl_xuat", "{0:n0}");
                        rpt.Tien_xuat.DataBindings.Add("Text", ds, "Tien_xuat", "{0:n0}");
                        rpt.Ton_cuoi.DataBindings.Add("Text", ds, "Ton_cuoi", "{0:n0}");
                        rpt.Du_cuoi.DataBindings.Add("Text", ds, "Du_cuoi", "{0:n0}");
                        rpt.Parameters["tungay"].Value = ngaybd;
                        rpt.Parameters["denngay"].Value = ngaykt;
                        if (drkhohang.SelectedValue == "")
                        {
                            rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";
                        }
                        else
                        {
                            rpt.Parameters["Kho"].Value = "KHO: " + drkhohang.SelectedValue + " - " + drkhohang.SelectedItem.Text.ToString();
                        }
                        rpt.Parameters["tenCty"].Value = TenCty;
                        rpt.Parameters["dcCty"].Value = DcCty;
                        rpt.Parameters["dtCty"].Value = DtCty;

                        rpt.Parameters["Tenpm"].Value = Tenpm;
                        rpt.Parameters["Loaipm"].Value = Loaipm;
                        rpt.Parameters["Webpm"].Value = Webpm;
                        lbError.Text = "";
                        Session["Report"] = rpt;
                    }
                }

                if (drmauin.SelectedValue == "R0091")
                {
                    string s = "EXEC [fs_HG_BCTHNXT]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', N'" + ma_hang + "',  N'" + nhom_hang + "',N'" + ma_kho + "',  N'" + nhom_kho + "',N'" + ma_vv + "',N'" + nhom_vv + "', " + ps + ", " + ton + "";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHNXT_Sl { DataSource = ds };
                    }
                    else
                    {
                        rpt = new rptTHNXT_Sl { DataSource = ds };
                    }
                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    if (drkhohang.SelectedValue == "")
                    {
                        rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";
                    }
                    else
                    {
                        rpt.Parameters["Kho"].Value = "KHO: " + drkhohang.SelectedValue + " - " + drkhohang.SelectedItem.Text.ToString();
                    }
                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;
                    lbError.Text = "";
                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0049")
                {
                    string s = "EXEC [fs_HG_BCTHNXTMR1]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', N'" + ma_hang + "',  N'" + nhom_hang + "',N'" + ma_kho + "',  N'" + nhom_kho + "',N'" + ma_vv + "',N'" + nhom_vv + "', " + ps + ", " + ton + "";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"].Copy();
                    dt.Columns.Add("Stt", typeof(string));
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["Stt"] = "" + (i + 1);
                    }
                    dt.AcceptChanges();
                    DataSet dsbcmr1 = new DataSet();
                    dsbcmr1.Tables.Add(dt);
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        m_rptTHNXTMR1 rpt = new m_rptTHNXTMR1 { DataSource = dsbcmr1 };
                        rpt.Stt.DataBindings.Add("Text", dsbcmr1, "Stt");
                        rpt.Parameters["tungay"].Value = ngaybd;
                        rpt.Parameters["denngay"].Value = ngaykt;
                        if (drkhohang.SelectedValue == "")
                        {
                            rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";
                        }
                        else
                        {
                            rpt.Parameters["Kho"].Value = "KHO: " + drkhohang.SelectedValue + " - " + drkhohang.SelectedItem.Text.ToString();
                        }
                        rpt.Parameters["tenCty"].Value = TenCty;
                        rpt.Parameters["dcCty"].Value = DcCty;
                        rpt.Parameters["dtCty"].Value = DtCty;

                        rpt.Parameters["Tenpm"].Value = Tenpm;
                        rpt.Parameters["Loaipm"].Value = Loaipm;
                        rpt.Parameters["Webpm"].Value = Webpm;
                        lbError.Text = "";
                        Session["Report"] = rpt;
                    }
                    else
                    {
                        rptTHNXTMR1 rpt = new rptTHNXTMR1 { DataSource = dsbcmr1 };
                        rpt.Stt.DataBindings.Add("Text", dsbcmr1, "Stt");
                        rpt.Parameters["tungay"].Value = ngaybd;
                        rpt.Parameters["denngay"].Value = ngaykt;
                        if (drkhohang.SelectedValue == "")
                        {
                            rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";
                        }
                        else
                        {
                            rpt.Parameters["Kho"].Value = "KHO: " + drkhohang.SelectedValue + " - " + drkhohang.SelectedItem.Text.ToString();
                        }
                        rpt.Parameters["tenCty"].Value = TenCty;
                        rpt.Parameters["dcCty"].Value = DcCty;
                        rpt.Parameters["dtCty"].Value = DtCty;

                        rpt.Parameters["Tenpm"].Value = Tenpm;
                        rpt.Parameters["Loaipm"].Value = Loaipm;
                        rpt.Parameters["Webpm"].Value = Webpm;
                        lbError.Text = "";
                        Session["Report"] = rpt;
                    }
                    
                }

                if (drmauin.SelectedValue == "R0118")
                {
                    string s = "EXEC [fs_HG_BCTHNXTNTKHO]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', N'" + ma_hang + "',  N'" + nhom_hang + "',N'" + ma_kho + "',  N'" + nhom_kho + "',N'" + ma_vv + "',N'" + nhom_vv + "', " + ps + ", " + ton + "";
                    DataSet dts = new DataSet();
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref dts);
                    DataTable dt = dts.Tables["bc"].Copy();
                    dt.Rows.RemoveAt(0);
                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHNXTKHO();
                    }
                    else
                    {
                        rpt = new rptTHNXTKHO();
                    }
                    rpt.DataSource = dt;
                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    if (drkhohang.SelectedValue == "")
                    {
                        rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";
                    }
                    else
                    {
                        rpt.Parameters["Kho"].Value = "KHO: " + drkhohang.SelectedValue + " - " + drkhohang.SelectedItem.Text.ToString();
                    }
                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;
                    lbError.Text = "";
                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0119")
                {
                    string s = "EXEC [fs_HG_BCTHNXTNTNH]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', N'" + ma_hang + "',  N'" + nhom_hang + "',N'" + ma_kho + "',  N'" + nhom_kho + "',N'" + ma_vv + "',N'" + nhom_vv + "', " + ps + ", " + ton + "";
                    DataSet dts = new DataSet();
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref dts);
                    DataTable dt = dts.Tables["bc"].Copy();
                    dt.Rows.RemoveAt(0);
                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHNXTNhHang();
                    }
                    else
                    {
                        rpt = new rptTHNXTNhHang();
                    }
                    rpt.DataSource = dt;
                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    if (drkhohang.SelectedValue == "")
                    {
                        rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";
                    }
                    else
                    {
                        rpt.Parameters["Kho"].Value = "KHO: " + drkhohang.SelectedValue + " - " + drkhohang.SelectedItem.Text.ToString();
                    }
                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;
                    lbError.Text = "";
                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0050")
                {
                    Session["Report"] = null;
                    lbError.Text = "Chưa có mẫu báo cáo";
                    return;
                }

                Response.Redirect("~\\Print.aspx");
            }
            catch (Exception ex)
            {
                return;
            }
        }

        protected void drnhomhang_SelectedIndexChanged(object sender, EventArgs objec)
        {
            txtMaHangHoa.Text = "";
            txtTenHangHoa.Text = "";
        }

        protected void drnhomkhohang_SelectedIndexChanged(object sender, EventArgs objec)
        {
            DataSet ds = new DataSet();
            if (drnhomkhohang.SelectedValue != "")
                hg3.hg3.sql.SQLRetrieve(appConn, "SELECT KhoID, KhoName FROM dmkho WHERE Status = '1' and FK_NhKhoID='" + drnhomkhohang.SelectedValue + "'", "dmkho", ref ds);
            else
                hg3.hg3.sql.SQLRetrieve(appConn, "SELECT KhoID, KhoName FROM dmkho WHERE Status = '1'", "dmkho", ref ds);
            DataTable dt = ds.Tables["dmkho"];
            DataRow newR = dt.NewRow();
            newR["KhoID"] = "";
            newR["KhoName"] = "Tất cả";
            dt.Rows.Add(newR);
            drkhohang.DataSource = dt;
            drkhohang.DataTextField = "KhoName";
            drkhohang.DataValueField = "KhoID";
            drkhohang.DataBind();
        }

        protected void drnhomvuviec_SelectedIndexChanged(object sender, EventArgs objec)
        {
            DataSet ds = new DataSet();
            if (drnhomvuviec.SelectedValue != "")
                hg3.hg3.sql.SQLRetrieve(appConn, "SELECT vvID, vvName FROM dmvv WHERE Status = '1' and FK_NhvvID='" + drnhomvuviec.SelectedValue + "'", "dmvv", ref ds);
            else
                hg3.hg3.sql.SQLRetrieve(appConn, "SELECT vvID, vvName FROM dmvv WHERE Status = '1'", "dmvv", ref ds);
            DataTable dt = ds.Tables["dmvv"];
            DataRow newR = dt.NewRow();
            newR["vvID"] = "";
            newR["vvName"] = "Tất cả";
            dt.Rows.Add(newR);
            drvuviec.DataSource = dt;
            drvuviec.DataTextField = "vvName";
            drvuviec.DataValueField = "vvID";
            drvuviec.DataBind();
        }

        private DataTable getNhomHang()
        {
            DataSet ds = new DataSet();
            hg3.hg3.sql.SQLRetrieve(appConn, "SELECT Nhvt2ID, Nhvt2Name FROM dmnhvt2 WHERE Status = '1'", "dmnhvt2", ref ds);
            DataTable dt = ds.Tables["dmnhvt2"];
            DataRow newR = dt.NewRow();
            newR["Nhvt2ID"] = "";
            newR["Nhvt2Name"] = "Tất cả";
            dt.Rows.Add(newR);
            return dt;
        }

        private DataTable getNhomKhoHang()
        {
            DataSet ds = new DataSet();
            hg3.hg3.sql.SQLRetrieve(appConn, "SELECT NhKhoID, NhKhoName FROM dmnhkho WHERE Status = '1'", "dmnhkho", ref ds);
            DataTable dt = ds.Tables["dmnhkho"];
            DataRow newR = dt.NewRow();
            newR["NhKhoID"] = "";
            newR["NhKhoName"] = "Tất cả";
            dt.Rows.Add(newR);
            return dt;
        }

        private DataTable getNhomVuViec()
        {
            DataSet ds = new DataSet();
            hg3.hg3.sql.SQLRetrieve(appConn, "SELECT NhvvID, NhvvName FROM dmnhvv WHERE Status = '1'", "dmnhvv", ref ds);
            DataTable dt = ds.Tables["dmnhvv"];
            DataRow newR = dt.NewRow();
            newR["NhvvID"] = "";
            newR["NhvvName"] = "Tất cả";
            dt.Rows.Add(newR);
            return dt;
        }

    }
}
